using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;

namespace Beauty.Net.Middleware
{
    public static class MiddlewareServiceCollectionExtensions
    {
        /// <summary>
        /// 添加全局日志中间件服务
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <returns>服务集合</returns>
        public static IServiceCollection AddGlobalLoggingMiddleware(this IServiceCollection services)
        {
            // 注册日志存储工厂
            services.AddSingleton<LogStorageFactory>();
            
            // 注册配置选项
            services.AddOptions<LoggingOptions>();
            
            // 注册默认的日志存储实现
            services.AddTransient<ILogStorage, ConsoleLogStorage>();
            
            return services;
        }

        /// <summary>
        /// 添加全局日志中间件服务，并从配置中绑定选项
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <param name="configuration">配置</param>
        /// <param name="sectionName">配置节名称</param>
        /// <returns>服务集合</returns>
        public static IServiceCollection AddGlobalLoggingMiddleware(
            this IServiceCollection services,
            IConfiguration configuration,
            string sectionName = "LoggingOptions")
        {
            services.AddOptions<LoggingOptions>()
                .Bind(configuration.GetSection(sectionName))
                .ValidateDataAnnotations();
            
            // 注册日志存储工厂
            services.AddSingleton<LogStorageFactory>();
            
            // 根据配置选择并注册日志存储实现
            services.AddTransient(sp => 
            {
                var options = sp.GetRequiredService<IOptions<LoggingOptions>>().Value;
                var factory = sp.GetRequiredService<LogStorageFactory>();
                return factory.CreateLogStorage(options);
            });
            
            return services;
        }

        /// <summary>
        /// 添加全局日志中间件服务，并使用配置委托
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <param name="configureOptions">配置选项的委托</param>
        /// <returns>服务集合</returns>
        public static IServiceCollection AddGlobalLoggingMiddleware(
            this IServiceCollection services,
            Action<LoggingOptions> configureOptions)
        {
            services.AddOptions<LoggingOptions>()
                .Configure(configureOptions)
                .ValidateDataAnnotations();
            
            // 注册日志存储工厂
            services.AddSingleton<LogStorageFactory>();
            
            // 根据配置选择并注册日志存储实现
            services.AddTransient(sp => 
            {
                var options = sp.GetRequiredService<IOptions<LoggingOptions>>().Value;
                var factory = sp.GetRequiredService<LogStorageFactory>();
                return factory.CreateLogStorage(options);
            });
            
            return services;
        }
    }
}